package com.mxx.bu_yang.common;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mxx.basic.activity.BaseActivity;
import com.mxx.basic.utils.SPUtils;
import com.mxx.bu_yang.R;
import com.mxx.bu_yang.response.LoginInfoResponse;

/**
 * 作者：刘江飞
 * 时间：2020/10/14  14:04
 * 邮箱：642371742Z@qq.com
 * 描述：通用标题栏
 **/
public abstract class CommonTitleActivity extends BaseActivity implements View.OnClickListener {
    protected FrameLayout fl_back;
    protected TextView tv_title;
    protected FrameLayout fl_right;
    protected ImageView iv_right;
    protected TextView tv_right;
    protected static final int TITLE_LEFT_BACK_MIDDLE_TITLE_RIGHT_NONE = 0;
    protected static final int TITLE_ONLY_LEFT_BACK = 1;
    protected static final int TITLE_LEFT_BACK_MIDDLE_TITLE_RIGHT_IMAGE = 2;
    protected static final int TITLE_LEFT_BACK_MIDDLE_TITLE_RIGHT_TEXT = 3;

    @Override
    public void initView() {
        fl_back = findViewById(R.id.fl_back);
        tv_title = findViewById(R.id.tv_title);
        fl_right = findViewById(R.id.fl_right);
        iv_right = findViewById(R.id.iv_right);
        tv_right = findViewById(R.id.tv_right);
        int title_type = getTitleType();
        if (title_type == TITLE_LEFT_BACK_MIDDLE_TITLE_RIGHT_NONE) {
            fl_right.setVisibility(View.GONE);
        }
        if (title_type == TITLE_ONLY_LEFT_BACK) {
            tv_title.setVisibility(View.GONE);
            fl_right.setVisibility(View.GONE);
        }
        if (title_type == TITLE_LEFT_BACK_MIDDLE_TITLE_RIGHT_IMAGE) {
            tv_right.setVisibility(View.GONE);
        }
        if (title_type == TITLE_LEFT_BACK_MIDDLE_TITLE_RIGHT_TEXT) {
            iv_right.setVisibility(View.GONE);
        }
    }

    protected abstract int getTitleType();

    @Override
    public void initListener() {
        super.initListener();
        fl_back.setOnClickListener(this);
        fl_right.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.fl_back) {
            if (!isSave()){
                finish();
            }else {
                saveResult();
            }
        }
        if (view.getId() == R.id.fl_right) {
            if (getTitleType() == TITLE_LEFT_BACK_MIDDLE_TITLE_RIGHT_IMAGE) {
                titleRightImageClick();
            }
            if (getTitleType() == TITLE_LEFT_BACK_MIDDLE_TITLE_RIGHT_TEXT) {
                titleRightTextClick();
            }

        }
    }

    protected boolean isSave() {
        return false;
    }

    protected  void saveResult(){

    }

    protected void titleRightImageClick() {
    }

    protected void titleRightTextClick() {
    }

    protected LoginInfoResponse getLoginInfo() {
        LoginInfoResponse response = null;
        String json = (String) SPUtils.get(this, "login_info", "");
        if (json != null && !json.isEmpty()) {
            response = new Gson().fromJson(json, new TypeToken<LoginInfoResponse>() {
            }.getType());
        }
        return response;
    }

    protected int getUser_Id() {
        LoginInfoResponse response = getLoginInfo();
        if (response == null) {
            return -1;
        }
        return response.userId;
    }

    protected int getWareId() {
        LoginInfoResponse response = getLoginInfo();
        if (response == null) {
            return -1;
        }
        return response.wareId;
    }

    protected String getWareName() {
        LoginInfoResponse response = getLoginInfo();
        if (response == null) {
            return "";
        }
        return response.wareName;
    }
}
